<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- bootstrap-validate -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css" />

    <!-- mui -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- 重置样式 -->
    <link rel="stylesheet/less" href="./assets/css/reset.less" />

    <!-- 公共样式 -->
    <link rel="stylesheet/less" href="./assets/css/common.less" />

    <!-- 页面独立的 -->
    <link rel="stylesheet/less" href="./assets/css/UserLogin.less" />
</head>
<body>
    <!-- 用户信息 -->
    <div class="user">
        <div class="avatar wow fadeInUp">
            <img src="./assets/images/avatar.png" />
        </div>
    </div>

    <form id="register" class="login wow fadeInUp" data-wow-delay="200ms">
        <div class="form-group">
            <label for="mobile">手机号</label>
            <input type="text" class="form-control" name="mobile" id="mobile" placeholder="请输入手机号" value="13800000000" />
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" name="password" id="password" placeholder="请输入密码" value="123" />
        </div>
        
        <div class="action">
            <button class="btn">注 册</button>
        </div>

        <div class="action">
            <a href="login.html" class="btn">登录 -></a>
        </div>
    </form>
</body>
</html>
<!-- 解析less的文件 -->
<script src="./assets/js/less.min.js"></script>

<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- bootstrap-validate -->
<script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>

<!-- mui -->
 <script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- axios -->
<script src="./assets/plugins/axios/axios.min.js"></script>

<!-- 服务请求 -->
<script src="./assets/plugins/axios/request.js"></script>

<script>
    //实例化wow.js
    new WOW().init();

    // 实例化验证器
    $('#register').bootstrapValidator({
        // 填写配置项
        message:'表单内容不能为空',// 所有字段默认错误信息
        // 字段校验图标。默认bootstrap图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields:{
            mobile:{
                validators:{
                    // 必填
                    notEmpty:{
                        message: '手机号不能为空'
                    },
                    // 校验字段长度
                    stringLength: {
                        min: 11,// 最小长度
                        max: 11,// 最大长度
                        message: '手机号必须是11位'
                    },
                    // 正则校验
                    regexp: {
                        regexp: /^1[3-9]\d{9}$/, 
                        message:'手机号格式错误'
                    }
                }
            },
            password:{
                validators:{
                    // 必填
                    notEmpty:{
                        message: '密码不能为空'
                    }
                }
            }
        }
    }).on('success.form.bv',function(e){
        // 阻止多次提交
        e.preventDefault();
    });

    // 表单提交事件
    $('#register').on('submit',async function(){
        // 判断表单验证是否失败
        if(!$('#register').data('bootstrapValidator').isValid())
        {
    
            // 验证没有通过就不执行后面代码
            return false;
        }

        // 获取手机号和密码并且组装数据
        let params = {
            mobile:$.trim($('#mobile').val()),
            password:$.trim($('#password').val())
        }

        // 发起请求
        let result = await POST({
            url:'/business/register',
            params
        })

        // 0 => 失败 1 => 成功
        if(result.code === 1)
        {
            // 提示信息
            mui.toast(result.msg,{duration:1500});

            // 设置1.5秒后跳转
            setTimeout(() => {
                location.href = 'login.html';
            },1500);

            return false;
        }else{
            // 提示错误信息
            mui.toast(result.msg,{duration:1500});

            return false;
        }
    })
</script>